
 /**
 * SessionCheckInterceptor.java.java.
 * User: BrianChen Date: 2012-7-20 下午05:39:04
 *
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * All rights reserved.
 */
package com.collapsar.core.aop.interceptor;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * <pre>
 * REVISIONS: 
 * Version 	Date 		Author 			Description
 * ------------------------------------------------------------------- 
 * 1.0 		2012-7-20 	BrianChen 	1. Created this class. 
 * </pre>
 * 
 * @author BrianChen
 * @date 2012-7-20 下午05:39:04
 * @version 1.0
 * @see
 * 
 */
@SuppressWarnings("serial")
public class SessionCheckInterceptor implements Interceptor{
	private static final Logger logger = LoggerFactory.getLogger(SessionCheckInterceptor.class);
	private String sessionAttribute = null;
	private String loginResult = null;

	public void setSessionAttribute(String sessionAttribute){
		this.sessionAttribute = sessionAttribute;
	}
	
	public void setLoginResult(String loginResult) {
		this.loginResult = loginResult;
	}
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub 
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub 
	}

	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		//读取session,判断session中是否有相应的attribute
		Map<String, Object> session = invocation.getInvocationContext().getSession();
		logger.info("----------------------a new request from user:"+session.get(sessionAttribute)+"------------------------");
		if(session.containsKey(sessionAttribute)){
			//logger.info("#current_user="+session.get(sessionAttribute));
			String resultCode = invocation.invoke();
			return resultCode;
		}else{
			return loginResult;
		}
	}
	

}

